//
//  WBNetWorkManager.swift
//  微博
//
//  Created by hejinyin on 2017/9/8.
//  Copyright © 2017年 WangQiang. All rights reserved.
//

import Alamofire

/// SnapKit约束闭包类型
//typealias GofConstraintMaker = (ConstraintMaker) -> Void;

/// 按钮点击回调闭包类型
typealias GofBtnClickBlock = @convention(block)(UIButton) -> Void;
//(UIButton) -> Void;

/// 只带一个error参数的失败闭包类型，主要用于处理失败回调
typealias GofErrorBlock = (NSError) -> Void;

/// 只带一个errorMessage参数的失败闭包类型，主要用于处理失败回调
typealias GofErrorMessageBlock = (String) -> Void;

/// 通用的空闭包类型，无参数，无返回值
typealias GofVoidBlock = (Void) -> Void;

/// 通知的闭包类型
typealias GofNotificationBlock = (NSNotification) -> Void;

/// 常用的返回Bool类型的闭包类型
typealias GofBoolBlock = (Bool) -> Void;

/// 常用的返回Bool类型和相应提示语的闭包类型
typealias GofBoolMsgBlock = (Bool, String) -> Void;

/// 常用的返回数组类型的闭包类型
typealias GofArrayBlock = ([Any]) -> Void;

/// 常用的返回数组类型和错误提示语的闭包类型
typealias GofArrayMsgBlock = ([Any], String) -> Void;

/// 常用的返回字典类型的闭包类型
typealias GofDictionaryBlock = ([String: Any]) -> Void;

/// 常用的返回字典类型和错误提示语的闭包类型
typealias GofDictionaryMsgBlock = ([String: Any], String) -> Void;

/// 常用的返回Int类型的闭包类型
typealias GofIntBlock = (Int) -> Void;

/// 常用的返回Int类型和错误提示语的闭包类型
typealias GofIntMsgBlock = (Int, String) -> Void;

/// 常用的返回String类型的闭包类型
typealias GofStringBlock = (String) -> Void;

/// 常用的返回String类型和错误提示语的闭包类型
typealias GofStringMsgBlock = (String, String) -> Void;

/// 常用的返回任何类型(包括函数类型）的闭包类型
typealias GofAnyBlock = (Any) -> Void;

/// 常用的返回任何类型(包括函数类型）和错误提示语的闭包类型
typealias GofAnyMsgBlock = (Any, String) -> Void;

/// 常用的返回任何类类型的闭包类型
typealias GofAnyObjectBlock = (AnyObject) -> Void;

/// 常用的返回任何类类型和错误提示语的闭包类型
typealias GofAnyObjectMsgBlock = (AnyObject, String) -> Void;

/// 常用的手势回调的闭包类型
typealias GofGestureBlock = (UIGestureRecognizer) -> Void;


typealias NetWorkRequesetCompeletion = ( AnyObject? ,  Error?) -> Void;

class WBNetWorkManager: SessionManager {
    
    
    //单例
    static var sharedInstance:WBNetWorkManager  {
        let instance = super.default
        instance.delegate.sessionDidReceiveChallenge = { session, challenge in
//            //认证服务器证书
//            if challenge.protectionSpace.authenticationMethod
//                == NSURLAuthenticationMethodServerTrust {
//                print("服务端证书认证！")
//                let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
//                let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
//                let remoteCertificateData
//                    = CFBridgingRetain(SecCertificateCopyData(certificate))!
//                let cerPath = Bundle.main.path(forResource: "server", ofType: "cer")!
//                let cerUrl = URL(fileURLWithPath:cerPath)
//                let localCertificateData = try! Data(contentsOf: cerUrl)
//                
//                if (remoteCertificateData.isEqual(localCertificateData) == true) {
//                    
//                    let credential = URLCredential(trust: serverTrust)
//                    challenge.sender?.use(credential, for: challenge)
//                    return (URLSession.AuthChallengeDisposition.useCredential,
//                            URLCredential(trust: challenge.protectionSpace.serverTrust!))
//                    
//                } else {
//                    return (.cancelAuthenticationChallenge, nil)
//                }
//            }
//                //认证客户端证书
//            else if challenge.protectionSpace.authenticationMethod
//                == NSURLAuthenticationMethodClientCertificate {
//                print("客户端证书认证！")
//                //获取客户端证书相关信息
//                let identityAndTrust:IdentityAndTrust = self.extractIdentity();
//                
//                let urlCredential:URLCredential = URLCredential(
//                    identity: identityAndTrust.identityRef,
//                    certificates: identityAndTrust.certArray as? [AnyObject],
//                    persistence: URLCredential.Persistence.forSession);
//                
//                return (.useCredential, urlCredential);
//            }
//                // 其它情况（不接受认证）
//            else {
//                print("其它情况（不接受认证）")
                return (.cancelAuthenticationChallenge, nil)
//            }
        }
        
        return instance as! WBNetWorkManager
    }
    
    func POST(_ url:String , parameters:[String : Any] , compeletion: NetWorkRequesetCompeletion) {
        
        request(url, method: .post, parameters: requestSerialization(for: parameters)).responseJSON { (response) in
//            responseSerialization(for: response)
            
        }
      
    }
    func GET(_ url:String , parameters:[String : Any] , compeletion: NetWorkRequesetCompeletion) {
        
    }
}
//MARK: =========== 请求和响应序列化 ===========
extension WBNetWorkManager{
   

    func requestSerialization( for params:[String : Any]? ) -> [String : Any]{
        var requestParam = params
        if requestParam == nil {
            requestParam = Dictionary<String,Any>.init()
        }
        
        return requestParam!
    }
    
    func responseSerialization(for response:AnyObject?) -> ([String:Any], Error?) {
        let result = Dictionary<String, Any>.init()
        
        return (result,nil)
        
    }
}
